package com.nari.ncps.screen.service.impl.wholeproduct;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nari.ncps.screen.entity.wholeproduct.WholeNeProdEffic;
import com.nari.ncps.screen.mapper.wholeproduct.WholeNeProdEfficMapper;
import com.nari.ncps.screen.service.wholeproduct.IWholeNeProdEfficService;
import com.nari.ncps.screen.vo.wholeproduct.AluminumProductionEfficiencyVO;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 铝业生产效率表 服务实现类
 * </p>
 *
 * @author xuesong
 * @since 2025-10-05
 */
@Service
public class WholeNeProdEfficServiceImpl extends ServiceImpl<WholeNeProdEfficMapper, WholeNeProdEffic> implements IWholeNeProdEfficService {
    
    @Override
    public AluminumProductionEfficiencyVO getLatestProductionEfficiency(Integer year) {
        // 构建查询条件
        QueryWrapper<WholeNeProdEffic> queryWrapper = new QueryWrapper<>();
        
        // 如果指定了年份，则根据年份查询
        if (year != null) {
            queryWrapper.eq("year", year);
        }
        
        // 按年份倒序排列，取最新的一条数据
        queryWrapper.orderByDesc("year").last("LIMIT 1");
        
        WholeNeProdEffic entity = this.getOne(queryWrapper);
        
        // 转换为VO对象
        AluminumProductionEfficiencyVO vo = new AluminumProductionEfficiencyVO();
        if (entity != null) {
            vo.setAioAvgConsumption(entity.getAioAvgConsumption());
            vo.setFlAvgConsumption(entity.getFlAvgConsumption());
            vo.setCaAvgConsumption(entity.getCaAvgConsumption());
        }
        
        return vo;
    }
}

